package com.qifan.alb2b.util;

import java.util.List;

import com.qifan.alb2b.domain.CountryInfoDomain;
import com.qifan.alb2b.domain.ExchangeInfoDomain;
import com.qifan.alb2b.service.cache.AppCache;

/**
 * 汇率工具
 * @author hc24
 *
 */
public class ExchangeUtil {
	
	/**
	 * 根据金额和国家转化成人民币（RMB）
	 * @param money 钱金额
	 * @param countryId 国家Id 
	 * @return 转换成人民币后的金额，四舍五入保留4位小数
	 */
	public static double changeMoney(double money,String countryId){
		int scale=4;//保留的小树位数
		List<CountryInfoDomain> countrys= AppCache.countryInfoList;
		String exchangeId = null;
		for(CountryInfoDomain c:countrys){
			if(c.getCountryId().equals(countryId.trim())){
				exchangeId=c.getExchangeId();
				break;
			}
		}
		ExchangeInfoDomain exchange=AppCache.exchangeInfoMap.get(exchangeId);
		if(exchange==null)
			throw new RuntimeException("货币转化错误，没找到该国家的汇率，请联系管理员");
		
		double result=Arith.roundHalfUp(Arith.mul(money, exchange.getExchangeRate()),scale);	
		return result;
	}
}
